package com.wxg.awt;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.UnsupportedEncodingException;

import org.junit.Test;

import com.wxg.kit.ResKit;

/**
 * 
 * @author pactera
 * 
 *         http://tool.oschina.net/uploads/apidocs/jdk-zh/java/awt/SystemTray.
 *         html 摘抄自"SystemTray"的API文档
 *
 *
 */
public class SystemTrayDemo {
	public static final String OS_ENCODING = System.getProperty("sun.jnu.encoding");

	public static void main(String[] args) {
		TrayIcon trayIcon = null;
		if (SystemTray.isSupported()) {
			SystemTray tray = SystemTray.getSystemTray();
			String imgpath = "imgs/Aatrox_Justicar_16px_1128770_easyicon.net.png";
			String imgName = ResKit.getRootResources(imgpath);
			System.out.println(String.format("imgName: %s", imgName));
			Image image = Toolkit.getDefaultToolkit().getImage(imgName);
			ActionListener listener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.out.println("hello,world!");
				}
			};
			PopupMenu popup = new PopupMenu();
			String itemLabel = "你好";
//			itemLabel = changeUtf8ToOsEncoding("你好");
			MenuItem defaultItem = new MenuItem(itemLabel);
			defaultItem.addActionListener(listener);
			MenuItem exitItem = new MenuItem("Exit");
			exitItem.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			});
			popup.add(defaultItem);
			popup.add(exitItem);
			// ... add other items
			trayIcon = new TrayIcon(image, "Tray Demo", popup);
			try {
				tray.add(trayIcon);
			} catch (AWTException e1) {
				e1.printStackTrace();
			}
		} else {

		}
	}

	public static String changeUtf8ToOsEncoding(String str) {
		byte[] byteArr = null;
		String retStr = null;
		try {
			byteArr = str.getBytes(OS_ENCODING);
			retStr = new String(byteArr, OS_ENCODING);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return retStr;
	}

	@Test
	public void test() {
		System.out.println(OS_ENCODING);
	}

	@Test
	public void encodeTest() throws Exception {
		String t = "中国";
		String utf8 = new String(t.getBytes("UTF-8"));
		System.out.println(String.format("1: %s", utf8));
		String unicode = new String(utf8.getBytes(), "UTF-8");
		System.out.println(String.format("2: %s", unicode));
		String gbk = new String(unicode.getBytes("GBK"));
		System.out.println(String.format("3: %s", gbk));
	}
	
	@Test
	public void encodeTest2() throws UnsupportedEncodingException {
		String s = "清山";
		byte[] b = s.getBytes("gbk");// 编码
		String sa = new String(b, "gbk");// 解码:用什么字符集编码就用什么字符集解码
		System.out.println(sa);
		b = sa.getBytes("utf-8");// 编码
		sa = new String(b, "utf-8");// 解码
		System.err.println(sa);
	}
	
}
